//
//  MessageCell.swift
//  HR
//
//  Created by zhenyu on 17/6/24.
//  Copyright © 2017年 allblue. All rights reserved.
//

import UIKit

class TimeView: UIView {
  
  var time = "" {
    didSet {
      timeLabel.text = time
    }
  }
  
  weak var timeLabel: UILabel!
  
  
  override init(frame: CGRect) {
    super.init(frame: frame)
    setView()
  }
  
  required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
  
  func setView() {
    backgroundColor = UIColor(hexString: "B5B5B5")
    
    layer.cornerRadius = 5
    
    timeLabel = UILabel().then({ (l) in
      addSubview(l)
      l.text = "2018年6月29日"
      l.textColor = UIColor.white
      l.font = HRFont(size: 12)
    }).then({ (l) in
      l.snp.makeConstraints({ (make) in
        make.center.equalTo(self)
      })
    })
  }
}


class MessageCell: UITableViewCell {
  
  weak var timeView: TimeView!
  weak var title: UILabel!
  weak var centerImage: UIImageView!
  weak var subTitle: UILabel!
  
  weak var bLeftDes: UILabel!
  
  class func identifier()-> String {
    return "MessageCell"
  }
  
  // MARK: - LifeCycle
  override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    selectionStyle = .none
    setViews()
  }
  
  required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
  
  override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
  }
  
  override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)
    // Configure the view for the selected state
  }
  
  
  // MARK: - SetViews
  func setViews() {
    backgroundColor = Color.back
    
    timeView = TimeView().then({ (v) in
      contentView.addSubview(v)
      v.snp.makeConstraints({ (make) in
        make.top.equalTo(30)
        make.centerX.equalTo(contentView)
        make.height.equalTo(23)
        make.width.equalTo(144)
      })
    })
    
    let whiteContent = UIView().then { (v) in
      contentView.addSubview(v)
      v.backgroundColor = UIColor.white
    }.then { (v) in
      v.snp.makeConstraints({ (make) in
        make.top.equalTo(timeView.snp.bottom).offset(17)
        make.left.equalTo(16)
        make.right.equalTo(contentView.snp.right).offset(-16)
        make.bottom.equalTo(contentView.snp.bottom)
      })
    }
    
    title = UILabel().then({ (l) in
      whiteContent.addSubview(l)
      l.numberOfLines = 0
      l.text = "BXT 技术沙龙第十期——全栈分析：共享单车违章停车的大数据计算"
      l.font = HRFont(size: 16)
      l.textColor = Color.font
    }).then({ (l) in
      l.snp.makeConstraints({ (make) in
        make.top.equalTo(20)
        make.left.equalTo(15)
        make.right.equalTo(whiteContent.snp.right).offset(-15)
      })
    })
    
    centerImage = UIImageView(image: #imageLiteral(resourceName: "m_test")).then({ (i) in
      whiteContent.addSubview(i)
      i.snp.makeConstraints({ (make) in
        make.top.equalTo(title.snp.bottom).offset(14)
        make.left.right.equalTo(title)
      })
    })
    
    subTitle = UILabel().then({ (l) in
      whiteContent.addSubview(l)
      l.numberOfLines = 0
      l.textColor = Color.lightFont
      l.text = "BXT分享会2017年第一发!"
      l.font = HRFont(size: 13)
    }).then({ (l) in
      l.snp.makeConstraints({ (make) in
        make.top.equalTo(centerImage.snp.bottom).offset(10)
        make.left.right.equalTo(centerImage)
      })
    })
    
    let line = UIView().then { (v) in
      whiteContent.addSubview(v)
      v.backgroundColor = Color.line
    }.then({ (v) in
      v.snp.makeConstraints({ (make) in
        make.top.equalTo(subTitle.snp.bottom).offset(10)
        make.left.right.equalTo(subTitle)
        make.height.equalTo(0.5)
      })
    })
    
    bLeftDes = UILabel().then({ (l) in
      whiteContent.addSubview(l)
      l.textColor = Color.font
      l.font = HRFont(size: 13, bold: true)
      l.text = "阅读全文"
    }).then({ (l) in
      l.snp.makeConstraints({ (make) in
        make.top.equalTo(line.snp.bottom).offset(18)
        make.left.equalTo(line.snp.left).offset(4)
        make.bottom.equalTo(whiteContent.snp.bottom).offset(-18)
      })
    })
    
    _ = UIImageView(image: #imageLiteral(resourceName: "m_arrow")).then({ (i) in
      whiteContent.addSubview(i)
      i.snp.makeConstraints({ (make) in
        make.centerY.equalTo(bLeftDes)
        make.right.equalTo(line.snp.right)
      })
    })
  }
}
